<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>已安装插件列表</title>
    <link href="/static/bootstrap/bootstrap.min.css?v=3.3.7" rel="stylesheet">
    <link href="../../css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="../../css/animate.min.css?v=4.0.0" rel="stylesheet">
    <link href="../../css/style.min.css?v=4.0.0" rel="stylesheet">

    <style>
        .descript {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-lin-clamp: 2;
            -webkit-box-orient: vertical;
        }
    </style>
</head>

<body class="gray-bg">
    <div class="container-fluid topmenu">
        <h2><i class="fa fa-cubes"></i> 插件宝库</h2>
    </div>
    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>已安装插件列表</h5>
                    </div>
                    <div class="ibox-content">

                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>序号</th>
                                    <th>插件名</th>
                                    <th>中文名称</th>
                                    <th>简介</th>
                                    <th>版本号</th>
                                    <th class="text-center">插件类型</th>
                                    <th class="text-center">安装&升级</th>
                                </tr>
                            </thead>
                            <tbody id="tabbody">

                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 全局js -->
    <script src="/static/jquery/jquery.min.js?v=2.1.4" type="text/javascript"></script>
    <script src="/static/bootstrap/bootstrap.min.js?v=3.3.7" type="text/javascript"></script>
    <script src="../../js/content.js?v=1.0.0"></script>

    <!-- 自定义全局js -->
    <script src="../../js-user/public.js" type="text/javascript"></script>

    <!-- 自定义js -->
    <script>
        $(function () {

            // 插件升级
            plugin_updata = function (thisobj, pluginName, upversion) {
                var urlapi = '../../py/plugin_update.py?op=update&r=' + Math.random();
                var postdata = {
                    name: pluginName,
                    version: upversion
                };

                $.ajax({
                    type: "POST",
                    url: urlapi,
                    data: postdata,
                    dataType: 'json',
                    beforeSend: function () {
                        thisobj.attr({ 'disabled': 'disabled' }).text('开始升级……');
                    },
                    success: function (jsonstr) {
                        if (jsonstr.code == '0000') {
                            parent.swal({ title: "恭喜您！", text: jsonstr.message, type: "success" }, function (value) {
                                thisobj.replaceWith('<span class="text-info">已安装最新版</span>');
                            });
                        } else {
                            parent.swal({ title: jsonstr.message, type: "error" }, function (value) {
                                thisobj.removeAttr('disabled').text('一键升级');
                            });
                        }
                    }
                });
            }

            // 插件安装
            plugin_install = function (thisobj, pluginName) {
                var urlapi = '../../py/plugin_update.py?op=install&r=' + Math.random();
                var postdata = {
                    name: pluginName
                };

                $.ajax({
                    type: "POST",
                    url: urlapi,
                    data: postdata,
                    dataType: 'json',
                    beforeSend: function () {
                        thisobj.attr({ 'disabled': 'disabled' }).text('开始安装……');
                    },
                    success: function (jsonstr) {

                        if (jsonstr.code == '0000') {
                            parent.swal({ title: "恭喜您！", text: jsonstr.message, type: "success" }, function (value) {
                                thisobj.replaceWith('<span class="text-info">已安装最新版</span>');
                            });
                        } else {
                            parent.swal({ title: jsonstr.message, type: "error" }, function (value) {
                                thisobj.removeAttr('disabled').text('一键安装');
                            });
                        }
                    }
                });
            }

            // 管理按钮事件
            button_click = function () {
                $('.pluginAdmin > a').click(function () {
                    var _this = $(this);
                    var text = $(this).text();
                    var name = $(this).data('name');
                    var version = $(this).data('version');

                    if (text == '一键升级') {
                        plugin_updata(_this, name, version);
                    }
                    if (text == '一键安装') {
                        plugin_install(_this, name)
                    }
                })
            };

            (load_pluginlist = function () {
                var urlapi = '../../py/plugin_list.py?op=getalllist&r=' + Math.random()
                $.ajax({
                    type: "POST",
                    url: urlapi,
                    dataType: 'json',
                    success: function (msg) {
                        if (msg.code == '0000') {
                            var data = msg.data;

                            var tabhtml = '';
                            $.each(data, function (i, n) {

                                tabhtml += '<tr><td>' + n.id + '</td><td>' + n.name + '</td><td>' + n.displayname + '</td>'
                                tabhtml += '<td class="descript">' + n.description.substr(0, 40);
                                if (n.description.length > 40) { tabhtml += '…'; }
                                tabhtml += '</td><td>' + n.version + '</td>';
                                tabhtml += '<td class="text-center">';
                                if (n.type == 1) {
                                    tabhtml += '<span class="text-success">官方插件</span>';
                                } else {
                                    tabhtml += '<span class="text-danger">第三方插件</span>';
                                }
                                tabhtml += '</td><td class="text-center pluginAdmin">';
                                if (n.state == '已安装') {
                                    tabhtml += '<span class="text-info">' + n.state + '最新版</span>';
                                } else if (n.state == '一键安装') {
                                    tabhtml += '<a class="btn btn-info btn-xs" data-name="' + n.name + '" data-version="' + n.version + '" href="javascript:void(0);" role="button">' + n.state + '</a>';
                                } else if (n.state == '一键升级') {
                                    tabhtml += '<a class="btn btn-danger btn-xs" data-name="' + n.name + '" data-version="' + n.version + '" href="javascript:void(0);" role="button">' + n.state + '</a>';
                                }
                                tabhtml += '</td></tr>';
                            });
                            $('#tabbody').html(tabhtml);
                            button_click();
                        }
                    }
                });
            })();
        });
    </script>
</body>

</html>